package com.dailyblue.example.project.bookstore.book.controller;

import com.dailyblue.example.project.bookstore.book.service.BookService;
import com.dailyblue.example.project.bookstore.commons.bean.Book;
import com.dailyblue.example.project.bookstore.commons.util.JsonResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookService service;

    @GetMapping("/{page}")
    public JsonResult find(@PathVariable int page) {
        return service.findBook(page);
    }

    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable String id) {
        return service.removeBook(id);
    }

    @PutMapping("/status/{id}/{status}")
    public JsonResult updateStatus(@PathVariable("id") String id, @PathVariable("status") int status) {
        return service.updateStatus(id, status);
    }

    @GetMapping("/{name}/{page}")
    public JsonResult find(@PathVariable String name, @PathVariable int page) {
        return service.findBookByName(name, page);
    }

    @PostMapping
    public JsonResult save(Book book) {
        return service.saveBook(book);
    }

    @GetMapping("/publisher")
    public JsonResult findAllPublisher() {
        return service.findAllPublisher();
    }

    @GetMapping("/front/fresh")
    public JsonResult findFreshAndGoodBooksLimit() {
        return service.findFreshAndGoodBooksLimit();
    }

    @GetMapping("/front/fresh/{page}")
    public JsonResult findFreshAndGoodBooks(@PathVariable int page) {
        return service.findFreshAndGoodBooks(page);
    }

    @GetMapping("/front/fresh/{name}/{page}")
    public JsonResult findFreshAndGoodBooks(@PathVariable String name, @PathVariable int page) {
        return service.searchFreshAndGoodBooks(name, page);
    }

    @GetMapping("/front/low")
    public JsonResult findLowPriceBooksLimit() {
        return service.findLowPriceBooksLimit();
    }

    @GetMapping("/front/low/{page}")
    public JsonResult findLowPriceBooks(@PathVariable int page) {
        return service.findLowPriceBooks(page);
    }

    @GetMapping("/front/low/{name}/{page}")
    public JsonResult searchLowPriceBooks(@PathVariable String name, @PathVariable int page) {
        return service.searchLowPriceBooks(name, page);
    }

    @GetMapping("/front/category/{id}/{page}")
    public JsonResult findBooksByCategoryId(@PathVariable String id, @PathVariable int page) {
        return service.findBooksByCategoryId(id, page);
    }

    @GetMapping("/front/category/{id}/{name}/{page}")
    public JsonResult searchCategoryIdBooks(@PathVariable String id, @PathVariable String name, @PathVariable int page) {
        return service.searchCategoryIdBooks(id, name, page);
    }

    @GetMapping("/front/id/{id}")
    public JsonResult findBookById(@PathVariable String id) {
        return service.findBookById(id);
    }


    @GetMapping("/front/{name}/{page}")
    public JsonResult frontFind(@PathVariable String name, @PathVariable int page) {
        return service.findBookByName(name, page);
    }
}
